package com.p2p.service.loanManagementService.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.p2p.common.DTO.LoanDTO;
import com.p2p.common.base.RespResult;
import com.p2p.common.base.RespResultCode;
import com.p2p.entity.Loan;
import com.p2p.mapper.LoanMapper;
import com.p2p.service.loanManagementService.LoanService;
import com.p2p.utils.FileUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class LoanServiceImpl extends ServiceImpl<LoanMapper,Loan>implements LoanService {
    @Autowired
    LoanMapper loanMapper;
    @Override
    public RespResult addLoan(Loan loan) {
        log.info("进入添加贷款信息");
        if(loan!=null){
            //生成时间戳为贷款编号
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSSS");
            String number = sdf.format(System.currentTimeMillis());//202211041552160219
            //补充一个三位随机数
            int x=(int)(Math.random()*900)+100;
            String loanNum = number + x;
            loan.setLoanNum(loanNum);
            loanMapper.insert(loan);
            log.info("添加贷款信息成功");
            return RespResult.success();
        }else {
            log.info("添加贷款信息失败");
            return RespResult.error("传入参数不能为空！！！");
        }
    }

    @Override
    public RespResult selectAllLoanList(Integer currentPage, Integer pageSize) {
             if(currentPage!=null&&pageSize!=null){
                 Page<LoanDTO> page = new Page(currentPage,pageSize);
                 List<LoanDTO> loanDTOS = loanMapper.selectAllLoanList(page);
                 Map<String,Object> map = new HashMap();
                 map.put("loanDTOS",loanDTOS);
                 map.put("page",page);
                 return RespResult.success(map);
             }
             return RespResult.error(RespResultCode.ERR_PARAM_NOT_LEGAL.getCode(), RespResultCode.ERR_PARAM_NOT_LEGAL.getMessage(),null);
    }

    @Override
    public RespResult updateLoan(Loan loan) {
        if(loan!=null && loan.getId()!=null){
            loanMapper.updateById(loan);
            return RespResult.success("修改成功！！！");
        }else {
            log.info("修改贷款信息失败");
            return RespResult.error("修改参数有误，请核对信息！！！");
        }
    }

    @Override
    public RespResult deleteLoan(Long id) {

        if(id!=null){
            loanMapper.deleteById(id);
            return RespResult.success("删除成功！！！");
        }else {
            log.info("删除贷款信息失败");
            return RespResult.error("删除参数有误，请核对信息！！！");
        }
    }

    @Override
    public RespResult selectLoanById(Long id) {
        if(id!=null){
            Loan loan = loanMapper.selectById(id);
            return RespResult.success(loan);
        }else {
            return RespResult.error("查询用户id不能为空！！！");
        }
    }

    @Override
    public RespResult findLoanByCondition(Integer currentPage, Integer pageSize, LoanDTO loanDTO) {
        if(currentPage!=null&&pageSize!=null){
            Page<LoanDTO> page = new Page(currentPage,pageSize);
            String title = loanDTO.getTitle();
            String borrower = loanDTO.getBorrower();
            String phone = loanDTO.getPhoneNum();
            Byte status = loanDTO.getLoanStatus();
            List<LoanDTO> loanDTOS = loanMapper.selectLoanByConditionList(page,title,borrower,phone,status);
            Map<String,Object> map = new HashMap();
            map.put("loanDTOS",loanDTOS);
            map.put("page",page);
            return RespResult.success(map);
        }
        return RespResult.error(RespResultCode.ERR_PARAM_NOT_LEGAL.getCode(), RespResultCode.ERR_PARAM_NOT_LEGAL.getMessage(),null);
    }

    @Override
    public RespResult addMaterialImage(MultipartFile file, Long id, HttpServletRequest request) {
        try {
          String imgPath = FileUtils.saveImage(file, request,FileUtils.LOAN_AVATAR);
          int result = loanMapper.addMaterialImg(imgPath,id);
            return RespResult.success("图片上传成功");
        } catch (Exception e) {
            e.printStackTrace();
            return RespResult.error("图片上传失败");
        }
    }

    @Override
    public RespResult addLoanInfoImg(MultipartFile file, Long id, HttpServletRequest request) {
        try {
            String imgPath = FileUtils.saveImage(file, request,FileUtils.LOAN_AVATAR);
            int result = loanMapper.addLoanInfoImg(imgPath,id);
            return RespResult.success("图片上传成功");
        } catch (Exception e) {
            e.printStackTrace();
            return RespResult.error("图片上传失败");
        }
    }

}
